package com.mjk.manager.api.interceptor;

import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.tools.interceptor.InterceptorChain;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.tools.springextend.BeeApplicationListener;
import com.mjk.common.util.MapUtils;
import com.mjk.common.web.getway.constns.GetwayConsts;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import com.mjk.common.web.getway.restful.interceptor.annotation.BeeGetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptorType;
import com.mjk.common.web.getway.utils.ResponseUtils;
import com.mjk.manager.api.apipost.ApiPostUtils;
import com.mjk.manager.api.apipost.IApiPostPush;
import com.mjk.manager.api.constns.ApiReturnType;
import com.mjk.manager.api.constns.ApiStatus;
import com.mjk.manager.api.model.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * API 接口返回拦截器，用于处理返回的数据
 * Apimodel 中returnType  normal jsondate xmldate
 * 此处 会和 BeeWork 中的 type 有冲突 所以 添加了beeapi 注解的接口 有冲突，
 * 如果在此处定义了就不会执行beework中的定义
 * @author msi
 * 
 */
@BeeGetWayInterceptor(value=10,interceptorType={GetWayInterceptorType.ALL})
public class ApiRetrunParamsInterceptor implements GetWayInterceptor, BeeApplicationListener {
	
	public final Map<String,String> returnTypeMap = new ConcurrentHashMap<>();
	
	@Autowired
	private BeeDBService beeDBService;


	@Autowired
	private IApiPostPush apiSync;

	@Override
	public BeeResponse interceptor(GetWayInterceptorReq request,
								   InterceptorChain<GetWayInterceptorReq, BeeResponse> next) {
		String work = request.getRestfulParams().get(GetwayConsts.INSTANCE).toString();
		String method ="";
		if(request.getParams().containsKey(GetwayConsts.METHOD)) {
			method = request.getParams().get(GetwayConsts.METHOD).toString();
		}else if(request.getRestfulParams().containsKey(GetwayConsts.METHOD)){
			method = request.getRestfulParams().get(GetwayConsts.METHOD).toString();
		}
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new ApiModel());
        wrapper.eq(ApiModel::getApiWork, work).eq(ApiModel::getApiMethod, method);
        ApiModel model = (ApiModel) beeDBService.findOneByWrapper(wrapper);

        if (model != null && ApiStatus.CLOSE.getType().equals(model.getStatus())) {
            return BeeError.SYSTEM_API_IS_CLOSE.tranceError();
        }

		BeeResponse response = next.proceed(request);
		try {
			if(null!=model)	{
				String sync = "apipost_sync";
				if(ApiPostUtils.APIPOST_PUSH.equals(MapUtils.getString(request.getParams(), sync))) {
					apiSync.push2ApiPost(model,request,response);
				}
				//记录请求日志
				//if(needRecord(response))
				//logService.put(new BeeLogModel(model.getId()+"",work,method,"1", GsonUtils.bean2Json(request.getParams()),GsonUtils.bean2Json(response),response.getCode()+""));
				if(ApiReturnType.JSONDATA.getType().equals(model.getReturnType())){
					ResponseUtils.returnGetWay(request.getResponse(), response.getData());
				}else if(ApiReturnType.XMLDATA.getType().equals(model.getReturnType())){
					ResponseUtils.returnGetWay(request.getResponse(), response.getData(),"3");
				}
			}else {
				apiSync.push2ApiPost(null,request,response);
			}
		}catch(Exception e) {//该处如果抛出异常会影响正常返回
			BizLogger.error(e);
		}
		return response;
	}


}
